home *** CD-ROM | disk | FTP | other *** search
/ Giga Games 1 / Giga Games.iso / net / usenet / volume3 / knight < prev    next >
Encoding:
Internet Message Format  |  1988-02-19  |  10.5 KB

  1. Path: uunet!husc6!mailrus!ames!ll-xn!mit-eddie!uw-beaver!tektronix!tekgen!tekred!games-request
  2. From: games-request@tekred.TEK.COM
  3. Newsgroups: comp.sources.games
  4. Subject: v03i085:  knight - knights tour, a chess oriented brain game
  5. Message-ID: <2195@tekred.TEK.COM>
  6. Date: 19 Feb 88 01:46:34 GMT
  7. Sender: billr@tekred.TEK.COM
  8. Lines: 367
  9. Approved: billr@tekred.TEK.COM
  10.  
  11. Submitted by: bobc@killer.UUCP (Bob Calbridge)
  12. Comp.sources.games: Volume 3, Issue 85
  13. Archive-name: knight
  14.  
  15.     [I had to add a little bit (#ifdef'd) to get this to compile
  16.      on my Sun, then had to add a few refresh() calls to see
  17.      the instructions. It compiles and runs OK now. (I assume it
  18.      worked OK on SYSV before.)   -br]
  19.  
  20. #! /bin/sh
  21. # This is a shell archive.  Remove anything before this line, then unpack
  22. # it by saving it into a file and typing "sh file".  To overwrite existing
  23. # files, type "sh file -c".  You can also feed this as standard input via
  24. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  25. # will see the following message at the end:
  26. #        "End of shell archive."
  27. # Contents:  README Makefile knight.c
  28. # Wrapped by billr@saab on Thu Feb 18 17:47:54 1988
  29. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  30. if test -f README -a "${1}" != "-c" ; then 
  31.   echo shar: Will not over-write existing file \"README\"
  32. else
  33. echo shar: Extracting \"README\" \(838 characters\)
  34. sed "s/^X//" >README <<'END_OF_README'
  35. X     The following program is derived from a game that was on a
  36. Xsystem where I used to work.  I don't know what the origins  or
  37. Xthe history game are. Instructions are included in the game  so
  38. Xthere is no need to explain  it here except  to mention that it 
  39. Xis a board game for one player.  It is actually more of a brain
  40. Xpuzzle.
  41. X     The only thing I can think of that would enhance the game is
  42. Xto make the program make the moves if there is only one move
  43. Xavailable from the current position.  I've run the game several
  44. Xtimes but so far I haven't been able to get all 64 squares so I
  45. Xdon't know for sure if the graphic representation for a winner 
  46. Xcomes out properly.  Logic says it does.  If anyone finds fault
  47. Xwith this program please send gripes or groans to bobc@killer.
  48. XAll suggestions appreciated.
  49. XBatteries not included.
  50. XBest,
  51. XBob
  52. END_OF_README
  53. if test 838 -ne `wc -c <README`; then
  54.     echo shar: \"README\" unpacked with wrong size!
  55. fi
  56. # end of overwriting check
  57. fi
  58. if test -f Makefile -a "${1}" != "-c" ; then 
  59.   echo shar: Will not over-write existing file \"Makefile\"
  60. else
  61. echo shar: Extracting \"Makefile\" \(201 characters\)
  62. sed "s/^X//" >Makefile <<'END_OF_Makefile'
  63. X# simple makefile for knight
  64. XCFLAGS = -O -DBSD    # Remove -DBSD if on SYSV or derivative
  65. XLIBS = -lcurses -ltermcap # don't need termcap for SYSV
  66. Xknight: knight.c
  67. X    cc -o knight $(CFLAGS) knight.c $(LIBS)
  68. END_OF_Makefile
  69. if test 201 -ne `wc -c <Makefile`; then
  70.     echo shar: \"Makefile\" unpacked with wrong size!
  71. fi
  72. # end of overwriting check
  73. fi
  74. if test -f knight.c -a "${1}" != "-c" ; then 
  75.   echo shar: Will not over-write existing file \"knight.c\"
  76. else
  77. echo shar: Extracting \"knight.c\" \(7138 characters\)
  78. sed "s/^X//" >knight.c <<'END_OF_knight.c'
  79. X/* Knights Tour - a brain game */
  80. X
  81. X#include <stdio.h>
  82. X#ifdef BSD
  83. X#include <ctype.h>
  84. X#endif
  85. X#include <curses.h>
  86. X#include <signal.h>
  87. X
  88. Xshort   board [64];     /* the squares */
  89. Xchar    row, column;    /* input characters */
  90. Xint     rw,col;         /* numeric equivalent of row and column */
  91. Xint     curow,curcol;   /* current row and column integers */
  92. Xint     rdif, cdif;     /* difference between input and current */
  93. Xint     j;              /* index into board */
  94. X
  95. Xchar    script[]={"'_)//,/(-)/__/__(_<_(__),|/|/_///_/_<//_/_)__o__o'______///_(--_(_)___,(_/,_/__(_\0"};
  96. X
  97. Xint    ypos[] ={1,0,1,2,3,0,1,2,2,3,3,2,2,3,2,2,3,3,3,3,3,2,3,3,2,4,5,5,
  98. X    4,3,3,2,1,2,3,3,3,2,1,3,3,2,3,3,3,2,1,1,0,1,4,4,4,4,4,4,5,6,7,7,
  99. X    7,6,6,6,7,7,7,6,6,6,6,7,7,7,6,7,7,6,6,7,7};
  100. X
  101. Xint    xpos[]={0,1,2,1,0,5,4,3,2,4,6,7,8,8,9,10,10,11,12,13,14,15,15,16,
  102. X    16,16,15,14,15,17,18,19,20,20,20,21,22,23,24,23,25,26,27,26,28,
  103. X    13,23,25,27,27,2,3,4,5,6,7,4,3,2,1,0,1,2,5,4,5,6,6,7,8,9,8,9,10,
  104. X    11,11,12,13,14,14,15};
  105. X
  106. Xstatic char *instructions[] = 
  107. X{
  108. X"     Knight's Tour is a board game for one player.   It is played on",
  109. X"an eight by eight board and is based on the allowable moves that a knight",
  110. X"can make in the game of chess.  For those who are unfamiliar with the",
  111. X"game, a knight may move either on a row or a column but not diagonally.",
  112. X"He may move one square in any direction and two squares in a perpendicular",
  113. X"direction >or< two squares in any direction and one square in a",
  114. X"perpendicular direction.  He may not, of course, move off the board.",
  115. X"",
  116. X"     At the beginning of a game you will be asked to either choose a",
  117. X"starting square or allow the computer to select a random location.",
  118. X"Squares are designated by a letter-number combination where the row is",
  119. X"specified by a letter A-H and the numbers 1-8 define a column.  Invalid",
  120. X"entries are ignored and illegal moves produce a beep at the terminal.",
  121. X"",
  122. X"     The objective is to visit every square on the board.  When you claim",
  123. X"a square a marker is placed on it to show where you've been.  You may",
  124. X"not revisit a square that you've landed on before.",
  125. X"",
  126. X"     After each move the program checks to see if you have any legal",
  127. X"moves left.  If not, the game ends and your squares are counted.  If",
  128. X"you've made all the squares you win the game.  Otherwise, you are told",
  129. X"the number of squares you did make.",
  130. X"END"
  131. X};
  132. X
  133. X
  134. Xmain ()
  135. X{
  136. X    init ();
  137. X    for (;;)  if (!play ()) {
  138. X            endwin ();
  139. X            exit (0);
  140. X    }
  141. X}
  142. X
  143. Xinit ()
  144. X{
  145. X    char c;
  146. X
  147. X    srand48 (getpid());
  148. X    initscr ();
  149. X    signal (SIGINT,SIG_IGN); 
  150. X    cbreak ();              /* immediate char return */
  151. X    noecho ();              /* no immediate echo */
  152. X    title (1,23);
  153. X    mvaddstr (23, 25, "Would you like instructions? ");
  154. X    refresh();
  155. X    if ((c=toupper (getch())) == 'Y') instruct();
  156. X    clear ();
  157. X}
  158. X
  159. Xplay ()
  160. X{
  161. X    drawboard ();           /* clear screen and drawboard */
  162. X    for (j = 0; j < 64; j++) board[j]=0;
  163. X    getfirst ();            /* get the starting square */
  164. X    for (;;) {
  165. X        getrc();
  166. X        if (evalmove()) {
  167. X            putstars ();
  168. X            if (!chkmoves()) return (endgame ());
  169. X        }
  170. X#ifdef BSD
  171. X        else putchar('\007');
  172. X#else
  173. X        else beep();
  174. X#endif
  175. X    }
  176. X}
  177. X
  178. Xdrawboard ()
  179. X{
  180. X    erase ();
  181. X    dosquares ();
  182. X    refresh ();
  183. X    mvaddstr (0, 7, "1   2   3   4   5   6   7   8");
  184. X    for (j = 0; j < 8; j++) mvaddch (2*j+2, 3, j + 'A');
  185. X    refresh ();
  186. X    mvaddstr (20,  5, "ROW:");
  187. X    mvaddstr (20, 27, "COLUMN:");
  188. X    mvaddstr (14, 49, "CURRENT ROW");
  189. X    mvaddstr (16, 49, "CURRENT COL");
  190. X    mvaddstr (22,  5, "A - H or Q to quit");
  191. X    mvaddstr (22, 27, "1 - 8 or ESC to cancel row");
  192. X    refresh ();
  193. X    title (1,40);
  194. X}
  195. X
  196. Xdosquares ()
  197. X{
  198. X    mvaddstr (1, 6, "-------------------------------");
  199. X    for (j = 1; j < 9; j++){
  200. X        mvaddstr (2*j, 5,  "|   |   |   |   |   |   |   |   |");
  201. X        mvaddstr (2*j+1, 6, "-------------------------------");
  202. X    }
  203. X}
  204. X
  205. Xgetfirst ()                             /* get first square */
  206. X{
  207. X    mvaddstr (23, 25, "(S)elect or (R)andom "); refresh ();
  208. X    do {
  209. X        row = toupper(getch());
  210. X    } while ((row != 'S') && (row != 'R'));
  211. X    if (row == 'R') {
  212. X        rw = lrand48() % 8;
  213. X        col = lrand48() % 8;
  214. X        row = rw + 'A';
  215. X        column = col + '1';
  216. X    }
  217. X    else {
  218. X        mvaddstr (23, 25, "Enter starting row and column");
  219. X        refresh ();
  220. X        getrc();                        /* get row and column */
  221. X    }
  222. X    putstars ();
  223. X    move (23, 0);
  224. X    clrtobot();
  225. X}       
  226. X
  227. Xgetrc ()                                /* get row and column */
  228. X{
  229. X    noecho ();
  230. X    do {
  231. X        mvaddstr (20, 35, "  ");
  232. X        refresh ();
  233. X        do {
  234. X            mvaddch (20, 11, ' ');
  235. X            move (20, 11);
  236. X            refresh ();
  237. X            row=toupper(getch());
  238. X            if (row == 'Q') {
  239. X                endwin ();
  240. X                exit (1);
  241. X            }
  242. X        } while ((row < 'A') || (row > 'H'));
  243. X        addch (row);
  244. X        move (20, 35);
  245. X        refresh ();
  246. X        do {
  247. X            column=getch();
  248. X            if (column == '\033') break;
  249. X        } while ((column < '1') || (column > '8'));
  250. X        if (column != '\033') addch (column);
  251. X    } while (column == '\033');
  252. X    refresh();
  253. X    rw = row - 'A';
  254. X    col= column - '1';
  255. X    j = 8 * rw + col;
  256. X}
  257. X
  258. Xputstars ()                     /* place the stars, update board & currents */
  259. X{
  260. X    mvaddch (2*curow+2, 38, ' ');
  261. X    mvaddch (2*rw+2, 38, '<');
  262. X    mvaddch (18, curcol*4+7, ' ');
  263. X    mvaddch (18, col*4+7, '^');
  264. X    curow = rw;
  265. X    curcol= col;
  266. X    mvaddstr (2 * rw + 2, 4*col+6, "***");
  267. X    mvaddch (14, 61, row);
  268. X    mvaddch (16, 61, column);
  269. X    refresh ();
  270. X    board[j] = 1;
  271. X}
  272. X
  273. Xevalmove()                      /* convert row and column to integers */
  274. X                        /* and evaluate move */
  275. X{
  276. X    rdif = rw - curow;
  277. X    cdif = col - curcol;
  278. X    rdif = abs(rw  - curow);
  279. X    cdif = abs(col - curcol);
  280. X    refresh ();
  281. X    if ((rdif == 1) && (cdif == 2)) if (board [j] == 0) return (1);
  282. X    if ((rdif == 2) && (cdif == 1)) if (board [j] == 0) return (1);
  283. X    return (0);
  284. X}
  285. X
  286. Xchkmoves ()                     /* check to see if valid moves are available */
  287. X{
  288. X    if (chksqr(2,1))   return (1);
  289. X    if (chksqr(2,-1))  return (1);
  290. X    if (chksqr(-2,1))  return (1);
  291. X    if (chksqr(-2,-1)) return (1);
  292. X    if (chksqr(1,2))   return (1);
  293. X    if (chksqr(1,-2))  return (1);
  294. X    if (chksqr(-1,2))  return (1);
  295. X    if (chksqr(-1,-2)) return (1);
  296. X    return (0);
  297. X}
  298. X
  299. Xendgame ()                      /* check for filled board or not */
  300. X{
  301. X    rw = 0;
  302. X    for (j = 0; j < 64; j++) if (board[j] != 0) rw+=1;
  303. X    if (rw == 64) mvaddstr (20, 20, "Congratulations !! You got 'em all");
  304. X        else mvprintw (20, 20, "You have ended up with %2d squares",rw);
  305. X    mvaddstr (21, 25, "Play again ? (y/n) ");
  306. X    refresh ();
  307. X    if ((row=tolower(getch())) == 'y') return (1);
  308. X        else return (0);
  309. X}
  310. X
  311. Xabs(num)
  312. Xint    num;
  313. X{
  314. X    if (num < 0) return (-num);
  315. X        else return (num);
  316. X}
  317. X
  318. Xchksqr (n1, n2)
  319. Xint    n1,n2;
  320. X
  321. X{
  322. X    int    r1, c1;
  323. X
  324. X    r1 = rw + n1;
  325. X    c1 = col + n2;
  326. X    if ((r1<0) || (r1>7)) return (0);
  327. X    if ((c1<0) || (c1>7)) return (0);
  328. X    if (board[r1*8+c1] == 0) return (1);
  329. X        else return (0);
  330. X}
  331. X
  332. Xinstruct()
  333. X{
  334. X    int i;
  335. X    char    c;
  336. X
  337. X    clear ();
  338. X    for (i=0;;i++) {
  339. X        if ((strcmp(instructions[i],"END"))) mvaddstr (i, 0, instructions[i]);
  340. X            else {
  341. X                mvaddstr (23, 25, "Ready to play ? (y/n) ");
  342. X                refresh();
  343. X                if ((c=toupper(getch())) == 'Y') {
  344. X                    clear ();
  345. X                    return (1);
  346. X                }
  347. X                    else {
  348. X                        clear ();
  349. X                        refresh ();
  350. X                        endwin ();
  351. X                        exit ();
  352. X                    }
  353. X            }
  354. X    }
  355. X}
  356. X
  357. Xtitle (y,x)
  358. Xint    y,x;
  359. X{
  360. X    char c;
  361. X    j = 0;
  362. X    do {
  363. X        c = script[j];
  364. X        if (c == 0) break ;
  365. X        mvaddch (ypos[j]+y, xpos[j]+x, c);
  366. X        refresh ();
  367. X        j++;
  368. X    } while (c != 0);
  369. X}
  370. END_OF_knight.c
  371. if test 7138 -ne `wc -c <knight.c`; then
  372.     echo shar: \"knight.c\" unpacked with wrong size!
  373. fi
  374. # end of overwriting check
  375. fi
  376. echo shar: End of shell archive.
  377. exit 0
  378.